package leetcode;

/**
 * Created by Ghost on 2015/1/31.
 */
public class Distinct_Subsequences {
    public int numDistinct(String S, String T) {
        if(S.length()==0 && T.length()==0)
            return 1;
        if(S.length()==0 || T.length()==0)
            return 0;
        char[] s = S.toCharArray();
        char[] t = T.toCharArray();
        int[][] w = new int[t.length][s.length];
        int sum = 0;
        for(int i=0;i<s.length;i++){
            if(t[0]==s[i]){
                sum++;
                w[0][i] = sum;
            } else
                w[0][i] = sum;
        }

        for(int i=1;i<t.length;i++)
            w[i][0]=0;
        for(int i=1;i<t.length;i++){
            sum=0;
            for(int j=1;j<s.length;j++){
                if(t[i]==s[j]){
                    sum+=w[i-1][j-1];
                    w[i][j] = sum;
                }
                else
                    w[i][j]=sum;
            }
        }

        return w[t.length-1][s.length-1];
    }
    public static void main(String[] args){
        Distinct_Subsequences distinct_subsequences  = new Distinct_Subsequences();
        System.out.println(distinct_subsequences.numDistinct("r",""));
    }
}
